<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


$file_conf['extract_save_dir'] = APP_CACHE_DIR;

$controller->loadClass('FileCategoryModel', 'file/category');
$controller->loadClass('RoleModel', 'role');

$controller->loadClass('FileEntry');
$controller->loadClass('FileEntryModel');
$controller->loadClass('FileEntryModel_dir');
$controller->loadClass('FileEntryView_common');
//$controller->loadClass('FileEntryModel_db');


// initialize objects
$rq =& new RequestData($_GET);
$rp =& new RequestData($_POST);

// settings
$setting = &new SettingModel();
$setting->setModuleId('file');
$setting =& $setting->getSettings();
$setting = array_merge($setting, $file_conf);

$obj = new FileEntry;

//$file_manager = 'FileEntryModel_' .  $setting['file_store'];
//$manager =& $obj->setManager( new $file_manager );

$manager =& $obj->setManager( new FileEntryModel_dir );
$manager->setFileSetting($setting);

$manager->checkPriv($priv, $controller->action, @$rq->id, $controller->getMoreParam('popup'));


switch ($controller->action) {
case 'delete': // ------------------------------
	
	if($manager->isEntryInUse($rq->id)) {
		$controller->go('notdeleteable_file', true);
	}
	
	if(isset($rp->submit_yes) || isset($rp->submit_no)) {
		$from_disk = (isset($rp->submit_yes));
		$manager->delete($rq->id, $from_disk);
		$controller->go();		
	}
	
	$data = &$manager->getById($rq->id);
	$rp->stripVarsValues($data);
	$obj->set($data);
	
	$view = &$controller->getView($obj, $manager, 'FileEntryView_delete');

	break;
	

case 'status': // ------------------------------
	
	$manager->status($rq->status, $rq->id);
	$controller->go();

	break;

	
case 'file': // ------------------------------
	
	$data = &$manager->getById($rq->id);
	$manager->sendFileDownload($data);
	exit;
	
	break;
	

case 'text': // ------------------------------
	
	if(isset($rp->submit)) {
		
		$rp->stripVars();
		$manager->updateFileText($rp->text, $rq->id);
		
		$controller->go();
		
	} else {
	
		$data = &$manager->getById($rq->id);
		$rp->stripVarsValues($data);
		$obj->set($data);
		
		$obj->text = &$manager->getFileText($rq->id);
		$rp->stripVarsValues($obj->text);
	}
	
	
	$view = &$controller->getView($obj, $manager, 'FileEntryView_text');

	break;
	
	
case 'update': // ------------------------------
case 'insert': // ------------------------------

	if(isset($rp->submit)) {
		
		$is_error =& $obj->validate($rp->vars, $controller->action);
		
		if($is_error) {
			$rp->stripVars(true);
			
			$obj->set($rp->vars);
			$obj->setCategory(@$rp->vars['category']);
			
			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
		
		} else {
			$rp->stripVars();
			
			$obj->set($rp->vars);
			$obj->setCategory($rp->vars['category']);
			
			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
					
			// with file
			if($_FILES['file_1']['name']) {
				
				$upload = &$manager->upload();
				//echo "<pre>"; print_r($upload); echo "</pre>";
				//exit;

				if(!empty($upload['error_msg'])) {
					$obj->error = $upload['error_msg'];
				
				} else {
					
					$content = &$manager->getFileContent($upload['good'][1]['to_read']);
					
					if($content) {
					
						$file_id = $manager->saveFileData($content, @$rq->id);
						
						$obj->set('id', $file_id);
						$obj->set('filename', addslashes($upload['good'][1]['name']));
						$obj->set('directory', $upload['good'][1]['directory']);
						$obj->set('filesize', $upload['good'][1]['size']);
						$obj->set('filetype', $upload['good'][1]['type']);
						
						if($manager->isExtra('file')) {

							if($manager->setting['file_extract']) {
								
								$controller->loadClass('FileTextExctractor');
								
								$extractor = &new FileTextExctractor($upload['good'][1]['extension']);
								$extractor->setTool($setting['extract_tool']);
								$extractor->setExtractDir($setting['extract_save_dir']);
								
								$obj->set('filetext', addslashes($extractor->getText($upload['good'][1]['to_read'])));
							}
						}
						
						$manager->save($obj, $controller->action, true);
						$controller->go();
		
					} else {
						$obj->errors[] = array('msg'=>'not_uploaded');
					}
				}
			
			// no file - only if update possible
			} else {
				
				$manager->save($obj, $controller->action, false);
				$controller->go();
			}
		}
		
	} elseif($controller->action == 'update') {
		
		$data = &$manager->getById($rq->id);
		$categories = $manager->getCategoryById($rq->id);
		
		$rp->stripVarsValues($data);
		$obj->set($data);
		$obj->setCategory($categories[1]);
		$obj->setCategoryMirror(@$categories[0]);
		
		if($manager->isExtra('private')) {
			$obj->setMemberRole($manager->getMemberRoleById($rq->id));
		}	
	}
	
	$view = &$controller->getView($obj, $manager, 'FileEntryView_form');

	break;	


default: // ------------------------------------
	
	$view = &$controller->getView($obj, $manager, 'FileEntryView_list');
}
?>